/**
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import parameter from '@ohos.systemparameter';
import LogUtil from '../../../../../../../common/utils/src/main/ets/default/baseUtil/LogUtil';

const MODULE_TAG: string = 'setting_vpn:Protocol:';

/**
 * support vpn protocol & displayname
 */
class ProtocolModel {
  IKEV2_IPSEC_MSCHAPv2: number = 0;
  IKEV2_IPSEC_PSK: number = 1;
  IKEV2_IPSEC_RSA: number = 2;
  L2TP_IPSEC_PSK: number = 3;
  L2TP_IPSEC_RSA: number = 4;
  IPSEC_XAUTH_PSK: number = 5;
  IPSEC_XAUTH_RSA: number = 6;
  IPSEC_HYBRID_RSA: number = 7;
  OPENVPN: number = 8;

  private protocols: [number, string][] = [
    [this.IKEV2_IPSEC_MSCHAPv2, 'lKEv2/IPSec MSCHAPv2'],
    [this.IKEV2_IPSEC_PSK, 'IKEv2/IPSEC PSK'],
    [this.IKEV2_IPSEC_RSA, 'IKEv2/IPSEC RSA'],
    [this.L2TP_IPSEC_PSK, 'L2TP/IPSec PSK'],
    [this.L2TP_IPSEC_RSA, 'L2TP/IPSec RSA'],
    [this.IPSEC_XAUTH_PSK, 'IPSEC Xauth PSK'],
    [this.IPSEC_XAUTH_RSA, 'IPSEC Xauth RSA'],
    [this.IPSEC_HYBRID_RSA, 'IPSEC Hybrid RSA'],
    [this.OPENVPN, 'OpenVpn']
  ];

  supportProtocols: number[] = [];
  supportDisplayNames: string[] = [];

  constructor() {
    let supportStr: string = parameter.getSync("persist.settings.supportVpn", "");
    LogUtil.info(MODULE_TAG + `supportVpn inputstr = ${supportStr}`);

    supportStr.split(',').forEach((p) => {
      let intP = Number(p);
      let isValidate: boolean = !isNaN(intP) && /^\d+$/.test(p) && intP >= 0 && intP <= this.OPENVPN;

      if (isValidate) {
        this.supportProtocols.push(intP);
        this.supportDisplayNames.push(this.protocols[intP][1]);
      }
    })
    if (this.supportProtocols.length == 0) {
      this.protocols.forEach((p: [number, string]) => {
        this.supportProtocols.push(p[0]);
        this.supportDisplayNames.push(p[1]);
      })
    }
    LogUtil.info(MODULE_TAG + `supportVpn result = ${this.supportProtocols} ${this.supportDisplayNames}`);
  }
}

let protocolModel = new ProtocolModel();
export default protocolModel;







